package com.example.customview.expandtextview

import android.annotation.SuppressLint
import android.content.Context
import android.os.Build
import android.text.Layout
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.StaticLayout
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.util.AttributeSet
import android.util.Log
import android.view.View
import androidx.core.content.ContextCompat
import com.android.baselibrary.utils.ScreenUtils
import com.example.customview.R
import com.google.android.material.internal.ViewUtils
/**
 * @author: Mr.You
 * @create: 2022-10-22 09:58
 * @description:
 **/

class IconTextView @JvmOverloads constructor(context: Context, attrs: AttributeSet?=null, defStyleAttr: Int=0): androidx.appcompat.widget.AppCompatTextView(context,attrs,defStyleAttr) {
init {
    maxLines = 2
    movementMethod = LinkMovementMethod.getInstance()
}
    private var SPAN_CLOSE = "  展开"

     fun setTexContent(content:CharSequence){
         val  text  = content.toString()
         var workText = StringBuffer(text).toString()
         val layout = createWorkingLayout(workText)
         val spannableString =  SpannableStringBuilder()
         val d = ContextCompat.getDrawable(context, R.drawable.cm_car_ad_edit)!!
         d.setBounds(0, 0, d.minimumWidth, d.minimumHeight)
         val imageSpan = MyIm(d)
         Log.i("TAGasd", "setTexContent: ${layout.lineCount}")
         if (layout.lineCount>2){
             Log.i("TAGasd1", "setTexContentsd: ${layout.getLineEnd(maxLines - 1)}")
             workText = text!!.substring(0, layout.getLineEnd(maxLines - 1))
                 .trim { it <= ' ' } // 收起状态原始文本截取展示的部分
             Log.i("TAGasd1", "setTexContent: ${workText}")
             val showText = text!!.substring(0, layout.getLineEnd(maxLines - 1))
                 .trim { it <= ' ' } + "..." + SPAN_CLOSE
             Log.i("TAGasd1", "setTexContent2: ${showText}")
             var layout2 = createWorkingLayout(showText)
             // 对workingText进行-1截取，直到展示行数==最大行数，并且添加 SPAN_CLOSE 后刚好占满最后一行
             while (layout2.lineCount > maxLines) {
                 val lastSpace = workText.length - 1
                 if (lastSpace == -1) {
                     break
                 }
                 workText= workText.substring(0, lastSpace)
                 layout2 = createWorkingLayout("$workText...$SPAN_CLOSE")
             }
             workText = "$workText... "
         }
             spannableString.append(workText).setSpan(imageSpan,spannableString.length -1,
                 spannableString.length, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
             spannableString.setSpan(object :ClickableSpan(){
                 override fun onClick(widget: View) {

                 }
             },spannableString.length-1,spannableString.length,Spanned.SPAN_INCLUSIVE_INCLUSIVE)
             setText(spannableString)


    }
    //返回textview的显示区域的layout，该textview的layout并不会显示出来，只是用其宽度来比较要显示的文字是否过长
    @SuppressLint("RestrictedApi")
    private fun createWorkingLayout(workingText: String?): Layout {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            StaticLayout(
                workingText,
                paint, ScreenUtils.getWidth(context)- ViewUtils.dpToPx(context, 60).toInt() - paddingLeft - paddingRight,
                Layout.Alignment.ALIGN_NORMAL,
                lineSpacingMultiplier, lineSpacingExtra, false
            )
        } else {
            StaticLayout(
                workingText, paint, ScreenUtils.getWidth(context)- ViewUtils.dpToPx(context, 60).toInt() - paddingLeft - paddingRight,
                Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false
            )
        }
    }
}